/**
 * 
 */
package com.luckmgr.web;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.common.constant.SystemConstant;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.luckmgr.entity.db.Company;
import com.luckmgr.entity.db.User;
import com.luckmgr.service.UserService;


/**
 * 
 * @author duanhx
 * @version [Ver 1.0; 2012-7-22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping("/account")
public class AccountController {
	
	@Autowired
	UserService userService;

	@RequestMapping("loginindex")
	public String loginindex() {
		return "login";
	}
	

	@RequestMapping("/login")
	public String userLogin(Model model,HttpSession session,HttpServletRequest request,String name,String pwd) {
		if(StringUtils.isNull(name) || StringUtils.isNull(pwd)){
			model.addAttribute("msg", "请输入用户名和密码");
			return "login";
		}
		User user = userService.loginUser(name);
		if(user==null){
			model.addAttribute("msg", "用户名或密码错误！请重新登录");
			return "login";
		}
		if(!pwd.equals(user.getUserPwd())){
			model.addAttribute("msg", "用户名或密码错误！请重新登录");
			user.setLoginNum(user.getLoginNum()+1);
			userService.updateEntity(user);
			return "login";
		}
		
		if(user.getLoginNum()>5){
			model.addAttribute("msg", "您帐号错误次数太多已被冻结，请联系管理员恢复！");
			user.setLoginNum(user.getLoginNum()+1);
			userService.updateEntity(user);
			return "login";
		}
		
		user.setLoginIp(getIpAddr(request));
		user.setLoginNum(0);
		user.setLoginTime(new Date());
		userService.updateEntity(user);
		session.setAttribute(SystemConstant.ESSIONUSER, user);
		return "redirect:/index";
	}
	
	
	@RequestMapping(value = "uppwd")
	public String uppwd(Model model,HttpSession session) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(null == user){
			model.addAttribute("msg", "帐户不存在！");
			return "account/uppwd";
		}
		model.addAttribute("user", user);
		return "account/uppwd";
	}
	
	
	@RequestMapping(value = "updatepwd")
	public String updatepwd(Model model,@ModelAttribute("user") User user) {
		if(null == user){
			model.addAttribute("msg", "帐户不存在！");
			return "account/uppwd";
		}
		if(0== user.getId() || StringUtils.isNull(user.getUserPwd())){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "account/uppwd";
		}
		userService.updateEntity(user);
		return "account/main";
	}
	
	
	@RequestMapping("/main")
	public String usermain(){
		return "account/main";
	}
	
	@RequestMapping("/out")
	public String userout(HttpSession session){
		session.setAttribute(SystemConstant.ESSIONUSER, null);
		return "redirect:/index";
	}
	
	
	
	
	private String getIpAddr(HttpServletRequest request) { 
	       String ip = request.getHeader("x-forwarded-for"); 
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	           ip = request.getHeader("Proxy-Client-IP"); 
	       } 
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	           ip = request.getHeader("WL-Proxy-Client-IP"); 
	       } 
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	           ip = request.getRemoteAddr(); 
	       } 
	       
	       if(!StringUtils.isNull(ip)) { 
	    	   if(ip.contains(",")){
	    		   String[] arrstr = ip.split(",");
	    		   if(null != arrstr && arrstr.length>0){
	    			   ip = arrstr[0];
	    		   }
	    	   }
	       }
	       return ip; 
	   } 
	
	
}
